import jieba

def readFile():
    file=open('test.txt','r',encoding='UTF-8').read()
    alphas=jieba.lcut(file)
    nums={}
    for word in alphas:
        if len(word)==1: 
            continue
        else:
            nums[word]=nums.get(word,0)+1
    items=list(nums.items())
    
    items.sort(key=lambda x:x[1],reverse=True)
    
    ff=open('output.txt','w+')
    for i in range(5):
        word,count=items[i]
        print ('{0:<10}{1:>5}'.format(word,count))
        #拼接字符串
        str_line=word+"\t"+str(count)
        # 写入一行
        ff.writelines(''.join(str_line)+'\n')

    ff.close()
readFile()
